package com.codeoss.module.stock.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.stock.dao.StockInventoryMapper;
import com.codeoss.module.stock.model.StockInventory;
import com.codeoss.module.stock.model.StockInventoryLine;
import com.codeoss.module.stock.model.StockTransaction;
import com.codeoss.module.stock.service.IStockInventoryLineService;
import com.codeoss.module.stock.service.IStockInventoryService;
import com.codeoss.module.stock.service.IStockStorageService;
import com.codeoss.module.stock.service.IStockTransactionService;

@Service
public class StockInventoryService extends BaseService<StockInventoryMapper, StockInventory>
		implements IStockInventoryService {
	@Autowired
	private IStockInventoryLineService stockInventoryLineService;
	@Autowired
	private IStockStorageService stockStorageService;
	@Autowired
	private IStockTransactionService stockTransactionService;
	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<StockInventoryLine> stockInventoryLineList=stockInventoryLineService.getLineListByStockInventoryId(id);
		if(stockInventoryLineList==null ||stockInventoryLineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		StockInventory stockInventory=getById(id);
		stockInventory.setDocStatus("CO");
		update(stockInventory);
		if(!stockInventory.getApproved()) {
			return;
		}
		List<StockInventoryLine> stockInventoryLineList=stockInventoryLineService.getLineListByStockInventoryId(id);
		for(int i=0;i<stockInventoryLineList.size();i++) {
			StockInventoryLine stockInventoryLine=stockInventoryLineList.get(i); 
			BigDecimal qtyDiff=stockInventoryLine.getQtyCount().subtract(stockInventoryLine.getQtyBook());
			if (qtyDiff.signum() == 0) {
				continue;
			}
			stockStorageService.add(stockInventory.getStockWarehouseId(), stockInventoryLine.getStockLocatorId(), 
					stockInventoryLine.getProductProductId(), stockInventoryLine.getProductAttrSetInstanceId(), qtyDiff, null, null);
			
			StockTransaction stockTransaction=new StockTransaction();
			String movementType=null;
			if(qtyDiff.signum()==1) {
				movementType="inventoryin";
			}else {
				movementType="inventoryout";
			}
			stockTransaction.setProductProductId(stockInventoryLine.getProductProductId());
			stockTransaction.setStockLocatorId(stockInventoryLine.getStockLocatorId());
			stockTransaction.setProductAttrSetInstanceId(stockInventoryLine.getProductAttrSetInstanceId());
			stockTransaction.setMovementDate(stockInventory.getMovementDate());
			stockTransaction.setMovementQty(qtyDiff);
			stockTransaction.setMovementType(movementType);
			stockTransaction.setStockInventoryLineId(stockInventoryLine.getId());
			stockTransactionService.insert(stockTransaction);
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		StockInventory stockInventory=getById(id);
		stockInventory.setApproved(true);
		update(stockInventory);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		StockInventory stockInventory=getById(id);
		stockInventory.setApproved(false);
		update(stockInventory);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		StockInventory stockInventory=getById(id);
		stockInventory.setDocStatus(docStatus);
		update(stockInventory);
	}
}